/* 封装一个指令用以实现倒计时 */
class loadingText{
    constructor($rootScope, $timeout){
        this.$rootScope = $rootScope;
        this.$timeout   = $timeout;
        this.restrict   = 'A';
    }

    link(scope, element, attrs, controller) {
        var text    = element.text(),
            number  = attrs.loadingText;

        var countdown = (num) => {
            element.text(num);
            var time = this.$timeout( () => {
                num--;
                if (num==0) {
                    this.$rootScope.loadingText = true;
                    element.text(text);
                    this.$timeout.cancel();
                    return false;
                }
                countdown(num);
            }, 1000);
        };

        element.bind('click', () => {
            if (this.$rootScope.loadingText) {
                scope.$apply( () => {
                    this.$rootScope.loadingText = false;
                    countdown(number);
                });   
            }
        });
        
    }
}

loadingText.$inject = ['$rootScope', '$timeout'];

export default loadingText;